// pages/memberdownload/memberdownload.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileimg: app.data.imgurl +"fileimg.png",
    getmyhuiyuaninfo: app.data.url + 'sheyingjia/Xhuiyuan1/getmyhuiyuaninfo',
    xiazaishengqinggeren: app.data.url + 'sheyingjia/Xhuiyuan1/xiazaishengqinggeren',
    yiyuedu: false,
    shuju: {},
    urls: '',
    progress: 0
  },

  //勾选
  gouxuan: function () {
    var yiyuedu = this.data.yiyuedu;
    if (yiyuedu) {
      yiyuedu = false;
    } else {
      yiyuedu = true;
    }
    this.setData({ yiyuedu: yiyuedu });
  },
  expressmail:function(){
    var that = this;
    var shuju = this.data.shuju;
    var yiyuedu = this.data.yiyuedu;
    if (!yiyuedu) {
      wx.showToast({
        title: '请下载团队入会申请',
        icon: 'none'
      })
      return;
    }
    var user = wx.getStorageSync('user');
    shuju.token = user.token;
    ea.request(that.data.xiazaishengqinggeren, shuju, function (res) {
      var data = res.data;
      console.log(data);
      if (data.code == 200) {
        wx.navigateTo({
          url: '/pages/memberexpressmail/memberexpressmail',
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
  },
  //下载
  xiazai:function(){
    var urls = this.data.urls;
    var that = this;
    // console.log(urls);
    wx.showLoading({
      title: '下载中',
    })
    var task = wx.downloadFile({
      url: urls,
      success: function (res) {
        console.log(res);
        if (res.statusCode == 200) {
          var tempFilePath = res.tempFilePath;
          console.log(tempFilePath)
          wx.saveFile({
            tempFilePath: tempFilePath,
            success: function (e) {
              console.log(e);
              wx.hideLoading();
              wx.showToast({
                title: '下载成功',
              })
            }
          })
          // wx.saveImageToPhotosAlbum({
          //   filePath:
          // })
        }
      }
    });
    task.onProgressUpdate(function (e) {
      // console.log(e);
      // that.setData({ progress: e.progress});
      wx.showLoading({
        title: e.progress + '%',
      })
    })
  },
  //团体入会细则
  togroup:function(){
    wx.navigateTo({
      url: '/pages/xqmembergroup/xqmembergroup',
    })
  },
  //管理办法
  togroups: function () {
    wx.navigateTo({
      url: '/pages/personalguanlibanfa/personalguanlibanfa',
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user');
    var that = this;
    ea.qinqiu(that.data.getmyhuiyuaninfo, { token: user.token, leixin:2},function(data){
      var urls = data.url;
      that.setData({ shuju: data.data });
      if (data.data.jindu != '填写信息') {
        wx.navigateTo({
          url: '/pages/memberexpressmail/memberexpressmail',
        })
      }
      that.setData({ urls: urls });
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})